//
// Created by zlj on 2024/11/18.
//
#include "app_adc.h"
#include "soft_config.h"
#include "pin_config.h"

/**
 *
 */
void ADC_GPIO_Configuration(void) {
    app_GPIO_Init(ADC_REF_PORT, ADC_REF_PIN, GPIO_INPUT, GPIO_PULL_DISABLE);
    adc_channel_gpio_config(ADC_REF_GPIO);						//配置ADC转换通道
    app_GPIO_Init(ADC_V_PORT, ADC_V_PIN, GPIO_INPUT, GPIO_PULL_DISABLE);
    adc_channel_gpio_config(ADC_V_GPIO);						//配置ADC转换通道
    app_GPIO_Init(ADC_I_PORT, ADC_I_PIN, GPIO_INPUT, GPIO_PULL_DISABLE);
    adc_channel_gpio_config(ADC_I_GPIO);						//配置ADC转换通道
}

/**
 *
 */
void ADC_Configuration(void) {
    adc_init(ADC_REF_VDDH, ADC_SAMPL_RATE_50K_SPS, ADC_SINGLE_MODE, ADC_REF_CH_EN | ADC_V_CH_EN | ADC_I_CH_EN);		//ADC初始化，选择ADC参考电压源、ADCCLK分频、采样模式和通道
    //adc_rxfifo_config(ADC_RXFIFO_LEAST_8DATA, ADC_CPU_ACCESS_RXFIFO, DISABLE);//不使能FIFO
    opa_init(ADC_MODE);
}

/**
 *
 * @param ch
 * @return
 */
uint16_t ADC_Get_Result(uint8_t ch) {
    uint8_t i;
    uint16_t ADResult;
    for (i = 0; i < 16; i++)
    {
        adc_convert_start();
        ADResult += adc_get_data(ch);
    }
    return (ADResult / 16);
}
